package com.agency8x.controller;

import com.agency8x.bean.JsonResultBean;
import com.agency8x.interceptor.CheckLoginInterceptor;
import com.agency8x.model.AuthRoleModel;
import com.agency8x.validator.AuthRoleValidator;
import com.agency8x.web.util.ControllerUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

/**
 * 权限角色页控制器
 * 
 * @author SoChishun
 *
 */
@Before(CheckLoginInterceptor.class)
public class AuthRoleController extends Controller {
	public void index() {
		String search_word = this.getPara("word");// 搜索
		setAttr("listPage", AuthRoleModel.me.paginate(getParaToInt(0, 1), 10, search_word));
		// 设置传入分页的url参数
		if (StrKit.notBlank(search_word)) {
			this.setAttr("append", "?word=" + search_word);
		}
		this.render("index.html");
	}

	public void edit() {
		Integer id = this.getParaToInt();
		if (null != id && id > 0) {
			AuthRoleModel model = AuthRoleModel.me.findById(id);
			this.setAttr("itemData", model);
		}
		this.render("edit.html");
	}

	@Before(AuthRoleValidator.class)
	public void doSave() {
		Integer id = this.getParaToInt("id");
		boolean result = false;
		AuthRoleModel model=getModel(AuthRoleModel.class,"");
		model.removeNullValueAttrs(); // 移除null属性
		if (null == id) {
			result = model.save();
		} else {
			result = model.update();
		}
		if (result) {
			ControllerUtil.renderDisptchJumpSuccess(this); // 操作成功，倒计时3秒后自动刷新列表页面
		} else {
			ControllerUtil.renderDisptchJumpError(this, "保存失败"); // 操作失败，倒计时3秒后，自动后退到提交的表单页面
		}
	}

	public void rule_edit() {
		Integer id = this.getParaToInt();
		if(null==id || id<1){
			renderText("参数无效!");
			return;
		}
		AuthRoleModel model=AuthRoleModel.me.findById(id);
		if(null==model){
			renderText("记录不存在!");
			return;			
		}
		this.setAttr("itemData", model);		
		this.render("rule_edit.html");
	}

	public void doSaveRule() {
		Integer id = this.getParaToInt("id");
		String rules = this.getPara("rules");
		if (null == id || null == rules) {
			ControllerUtil.renderDisptchJumpError(this, "表单数据无效!");
			return;
		}
		boolean result = AuthRoleModel.me.saveRules(id, rules);
		if (result) {
			ControllerUtil.renderDisptchJumpSuccess(this); // 操作成功，倒计时3秒后自动刷新列表页面
		} else {
			ControllerUtil.renderDisptchJumpError(this, "保存失败"); // 操作失败，倒计时3秒后，自动后退到提交的表单页面
		}
	}

	/**
	 * 删除操作 <br />
	 * 支持单个删除和批量删除 <br />
	 * 单个删除：?id=1 <br />
	 * 批量删除：?id=1,2,3
	 * 
	 * @since 1.0 2017-5-15
	 */
	public void doDelete() {
		boolean result = false;
		Integer id=this.getParaToInt();
		if (null != id) {
			result = AuthRoleModel.me.deleteById(id);
		}
		this.renderJson(new JsonResultBean(result, "删除" + (result ? "成功" : "失败")));
	}	
	
	public void changeStatus(){
		Integer id=this.getParaToInt("id");
		Integer status=this.getParaToInt("status");
		boolean result=AuthRoleModel.me.changeStatus(id, status);
		this.renderJson("{\"status\":true}");
	}
	
	public void changeSort(){
		Integer id=this.getParaToInt("id");
		Integer sort=this.getParaToInt("sort");
		boolean result=AuthRoleModel.me.changeSort(id, sort);
		this.renderJson("{\"status\":true}");
	}
}
